<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Hybrid Exercise
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial128.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial121.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc256">17.8</A>&nbsp;&nbsp;Hybrid Exercise</H2>
Build a hybrid algorithm to create lists whose elements all differ by
at least 2. Try lists of length 3,5,7,8.
To test its performance, reduce the domains thus:
<CODE>ic:(List::1..TwoL-2)</CODE>
so the program tries all possibilities before failing. <BR>
<BR>
Use the following skeleton:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
differ(Length,List) :-
    length(List,Length),
    TwoL is 2*Length,
    ic:(List::1..TwoL-1),
    alldiff(List,TwoL,Bools),
    [To be completed]

alldiff(List,Length,Bools) :-
    (  fromto(List,[X|Rest],Rest,[]),
       fromto([],BIn,BOut,Bools),
       param(Length)
    do
        diffeach(X,Rest,Length,BIn,BOut)
    ).
    
diffeach(X,List,Length,BIn,BOut) :-
        (foreach(Y,List),
         fromto(BIn,TB,[B|TB],BOut),
         param(X,Length)
        do
            diff2(X,Y,Length,B)
        ).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
<UL CLASS="itemize"><LI CLASS="li-itemize">
(a) Create an IC algorithm using
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
diff2(X,Y,_,_) :- ic: ((X+2 #=&lt; Y) or (Y+2 #=&lt; X)).
</PRE></BLOCKQUOTE><BR>
<BR>
<LI CLASS="li-itemize">(b) Create an eplex algorithm using
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
diff2(X,Y,Max,B) :- 
    eplex:(B::0..1),
    eplex:( X+2 + B*Max $=&lt; Y+Max),
    eplex:(X+Max $&gt;= Y+2 + (1-B)*Max).
</PRE></BLOCKQUOTE>
<LI CLASS="li-itemize">(c) Try and find the best hybrid algorithm.
 (NB This is, unfortunately, a trick question ;-))</UL>
<HR>
<A HREF="tutorial128.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial121.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
</BODY>
</HTML>
